package com.eascs.banknet.boc.service.ftp;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.banknet.boc.dao.DataListDao;
import com.eascs.banknet.boc.dao.LoanapplyDao;
import com.eascs.banknet.boc.service.BOCAbstractService;
import com.eascs.banknet.common.entity.DataListEntity;
import com.eascs.banknet.common.entity.LoanUserEntity;
import com.eascs.banknet.constant.FileTypeConstants;
import com.eascs.banknet.log.entity.LogEntity;

@Service("bOCUserFileUploadService")
public class BOCUserFileUploadService extends BOCAbstractService {
	private static final Logger logger = LoggerFactory.getLogger(BOCUserFileUploadService.class);
	@Autowired
	private LoanapplyDao loanapplyDao;
	@Autowired
	private DataListDao dataListDao;

	@Override
	public Map<String, String> dowork() {
		logger.info("开始上传中行申报资料,跑批开始...");
		List<LoanUserEntity> dataList = loanapplyDao.getSendBankFtpApplyList();
		if (dataList.isEmpty()) {
			logger.info("上传中行申报资料， 没有符合条件的数据");
			return null;
		}
		LogEntity logEntity = initLogData();
		for (LoanUserEntity entity : dataList) {
			String appno = null;
			try {
				appno = entity.getBankApplyno();
				List<DataListEntity> fileList = dataListDao.getFileDataList(entity.getLoanapplyNo(), Arrays.asList(FileTypeConstants.DALIST_TYPE_BANKAPPLYDATA));
				bocNetService.sendFtpAttachment(appno, fileList);
				logService.writeLog(logEntity, appno, LogEntity.RESULT_TYPE_SUCCESS, null);
				logger.info("appno:[{}],上传中行申报资料成功", appno);
			} catch (Exception e) {
				logService.writeLog(logEntity, appno, LogEntity.RESULT_TYPE_ERROR, e.getMessage());
				logger.error("上传中行申报资料出现异常，appno:[{}], errMsg：[{}]", appno, e);
				e.printStackTrace();
			}
		}
		logger.info("开始上传中行申报资料,跑批结束...");
		return null;
	}

	@Override
	public LogEntity initLogData() {
		LogEntity logEntity = new LogEntity();
		logEntity.setBankType(LogEntity.BANK_TYPE_BOC);
		logEntity.setTransType("USER_FILE");
		logEntity.setTypeName("上传中行申报资料");
		logEntity.setStartTime(new Date());
		return logEntity;
	}
}
